package cn.doitedu.day07

import org.apache.spark.sql.SparkSession

import java.util.Properties

object T14_WriteToCsv {

  def main(args: Array[String]): Unit = {

    val spark = SparkSession.builder().appName(this.getClass.getSimpleName)
      .master("local[4]")
      .getOrCreate()

    //mysql的URL地址
    val url = "jdbc:mysql://node-1.51doit.cn:3306/doit?characterEncoding=utf-8"
    val table = "tb_user"
    val properties = new Properties()
    //指定MySQL的用户名和密码
    properties.setProperty("user", "root")
    properties.setProperty("password", "123456")

    //在Driver端直接跟MySQL建立连接，不是为了读取数据，而是为了获取schema信息
    val df = spark.read.jdbc(url, table, properties)

    //将dataFrame中的数据写成csv格式
    df.write
      .option("header", "true")
      .option("delimiter", "|")
      .csv("out/csv2")

  }

}
